<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Parser
*/
class DomArClassParserRecordProperty_Field extends DomArClassParserRecordProperty {
	
	protected $type;
	
	protected $param1;
	
	protected $param2;
	
	protected $index;
	
	protected $null = true;
	
	protected $empty = true;
	
	protected function setOrm( array &$orm ) {
		$this->type =  strtolower($orm['type']);
		$this->param1 =  $orm['length'][0];
		$this->param2 =  $orm['length'][1];
		$this->index = $orm['index'];
		
		$orm['notNull'] = strtolower($orm['notNull']);
		if( $orm['notNull'] == 'not null') {
			$this->null = false;
		} else if( $orm['notNull'] == 'not empty' ) {
			$this->null = false;
			$this->empty = false;
		}
	}
	
	protected function updateHandlerClassName( DomArClassParser $class ) {
		switch( $this->type ) {
			case 'text':
				$this->handlerClassName = 'DomArProperty_Text';
			break;
			case 'char':
				$this->handlerClassName = 'DomArProperty_Char';
			break;
			case 'longtext':
				$this->handlerClassName = 'DomArProperty_LongText';
			break;
			case 'int':
				$this->handlerClassName = 'DomArProperty_Int';
			break;
			case 'float':
				$this->handlerClassName = 'DomArProperty_Float';
			break;
			case 'bool':
				$this->handlerClassName = 'DomArProperty_Bool';
			break;
			case 'boolean':
				$this->handlerClassName = 'DomArProperty_Bool';
			break;
			case 'enum':
				$this->handlerClassName = 'DomArProperty_Enum';
			break;
			case 'set':
				$this->handlerClassName = 'DomArProperty_Set';
			break;
			case 'array':
				$this->handlerClassName = 'DomArProperty_Array';
			break;
			case 'email':
				$this->handlerClassName = 'DomArProperty_Email';
			break;
			case 'password':
				$this->handlerClassName = 'DomArProperty_Password';
			break;
			default:
				throw new RuntimeException('Unknown property '.$this->name.' type '.strtolower($this->type).'');
			break;
		}
	}
	
	protected function updateCreateHandlerCode( DomArClassParser $class ) {
		switch( $this->handlerClassName ) {
			case 'DomArProperty_Text':
				$this->createHandlerCode = "new DomArProperty_Text('".$this->name."',".($this->null?'true':'false').",".($this->empty?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_LongText':
				$this->createHandlerCode = "new DomArProperty_LongText('".$this->name."',".($this->null?'true':'false').",".($this->empty?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Char':
				$this->createHandlerCode = "new DomArProperty_Char('".$this->name."','".$this->param1."',".($this->null?'true':'false').",".($this->empty?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Int':
				$this->createHandlerCode = "new DomArProperty_Int('".$this->name."','".$this->param1."',".($this->null?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Float':
				$this->createHandlerCode = "new DomArProperty_Float('".$this->name."','".$this->param1."','".$this->param2."',".($this->null?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Bool':
				$this->createHandlerCode = "new DomArProperty_Bool('".$this->name."',".($this->null?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Enum':
				$values = 'array(\''.implode('\',\'', $this->param1).'\')';
				$this->createHandlerCode = "new DomArProperty_Enum('".$this->name."',".$values.",'".$this->index."')";
			break;
			case 'DomArProperty_Set':
				$values = 'array(\''.implode('\',\'', $this->param1).'\')';
				$this->createHandlerCode = "new DomArProperty_Set('".$this->name."',".$values.",'".$this->index."')";
			break;
			case 'DomArProperty_Array':
				$this->createHandlerCode = "new DomArProperty_Array('".$this->name."',true,NULL)";
			break;
			case 'DomArProperty_Email':
				$this->createHandlerCode = "new DomArProperty_Email('".$this->name."',".($this->null?'true':'false').",".($this->empty?'true':'false').",'".$this->index."')";
			break;
			case 'DomArProperty_Password':
				$this->createHandlerCode = "new DomArProperty_Password('".$this->name."',".($this->null?'true':'false').",".($this->empty?'true':'false').",'".$this->param1."','".$this->param2."')";
			break;
			default:
				throw new RuntimeException('Unknown handler classname for '.$this->name.' type '.strtolower($this->type).'');
			break;
		}
	}

}

?>